package pers.gxm.chainofresponsibility;



public abstract class Support {
    private String name;
    private Support next;
    public Support(String name){
        this.name = name;
    }
    public Support setNext(Support next){
        this.next = next;
        return next;
    }
    public final void support1(Trouble trouble) {
        if(resolve(trouble)){
            done(trouble);
        }else if(next != null){
            next.support(trouble);
        }else{
            fail(trouble);
        }
    }

    public final void support(Trouble trouble){
        Support curr = this;
        while(curr != null){
            if(curr.resolve(trouble)){
                curr.done(trouble);
                return;
            }else if(curr.next == null){
                curr.fail(trouble);
                return;
            }
            curr = curr.next;

        }
//        this.fail(trouble);
    }

    @Override
    public String toString() {
        return "Support{" +
                "name='" + name + '\'' +
                '}';
    }
    protected abstract boolean resolve(Trouble trouble);

    protected  void done(Trouble trouble){
        System.out.println( trouble + "is resolved by" + this + ".");
    }
    protected  void fail(Trouble trouble){
        System.out.println(trouble + "cannot be resolved.");
    }
}
